import React, {PureComponent} from 'react';
import {Table, Alert, Badge, Switch, Icon, InputNumber, Tooltip, Popconfirm,Button} from 'antd';
import styles from './index.less';
import constants from '../../../common/constants';
import {powerControl} from '../../../utils/powerControl';
const AuthButton = powerControl(Button);

export default class RecObjectList extends PureComponent{
    state = {
        selectedRowKeys: [],
        totaCallNo: 0,
        expandedRowKeys: []
    };

    componentWillReceiveProps(nextProps){
        if (nextProps.selectedRows.length === 0){
            this.setState({
                selectedRowsKeys: [],
                totalCallNo: 0,
            });
        }
    };

    handleEditRow = (record, pagination) => {
        this.props.onEditRow(record, pagination);
    };

    handleDelRow = (priority_id, pagination) => {
        this.props.onDelRow(priority_id, pagination);
    };

    handleTableChange = (pagination, filters, sorter) => {
        this.props.onChange(pagination, filters, sorter);
    };

    onExpandedRowsChange =(expandedRows) =>{

        let expanded = [];
        expandedRows.map(function(k,index){
            if (index === expandedRows.length - 1){
                expanded.push(k);
            }

        });
        this.setState({
            expandedRowKeys: expanded
        })
    };

    render(){
        const {selectedRowKeys, totaCallNo} = this.state;
        const {data: {list, pagination}, loading, expandedRowRender} = this.props;
        const paginationProps = {
            showSizeChanger: true,
            showQuickJumper: true,
            ...pagination,
        };
        const rowSelection = {
            onChange: (selectedRowKeys, selectedRows) => {
                console.log(`selectedRowKeys: ${selectedRowKeys}`, 'selectedRows: ', selectedRows);
            },
            onSelect: (record, selected, selectedRows) => {
                console.log(record, selected, selectedRows);
            },
            onSelectAll: (selected, selectedRows, changeRows) => {
                console.log(selected, selectedRows, changeRows);
            },
        };
        const columns = [{
            title: '位置名称',
            dataIndex: 'seat_name',

        },{
            title: '添加人员',
            dataIndex: 'user_name',

        },{
            title: '转入详情ID',
            dataIndex: 'detail_url',

        },{
            title: '标题',
            dataIndex: 'obj_title'
        },{
            title: '图片',
            dataIndex: 'obj_img',
            render: (val, record) => {
                return<img src={`${constants.imageUrlFix}${val}`} style={{width:50,height:50}}/>

            }
        },{
            title: '添加时间',
            dataIndex: 'add_time',
        },{
            title: '推荐对象名称',
            dataIndex: 'obj_name',
        },{
            title: '推荐对象类型',
            dataIndex: 'obj_type',
            render: (val) =>{
                return<span>{val === 1 ? '商品' : (val===2 ? '品牌':val===4?'分类':'自定义')}</span>
            }
        },{
            title: '状态',
            dataIndex: 'status',//0不在用，1再用
            render: (val) => {
                return <Switch checkedChildren={<Icon type="check"/>}
                               unCheckedChildren={<Icon type="cross"/>}
                               defaultChecked={val}
                               checked={val}/>
            },
        },{
            title: '生效时间',
            dataIndex: 'start_time',

        },{
            title: '失效时间',
            dataIndex: 'end_time',
        },{
            title: '操作',
             fixed: 'right',
            render: (val, record) => (
                <p>
                    <AuthButton auth="recObject.edit" ghost  className={styles.btn} size="small" onClick={() => this.handleEditRow(record, pagination)}>编辑</AuthButton>
                    <span className={styles.splitLine}/>
                    <Popconfirm title="是否确定删除?" onConfirm={() => this.handleDelRow(record.priority_id, pagination)} >
                        <AuthButton auth="recObject.delete" ghost  className={styles.btn} size="small">删除</AuthButton>
                    </Popconfirm>
                </p>
            ),
        }];
        return(
            <div className={styles.standardTable}>
                <Table
                    loading={loading}
                    rowKey={record=>record.priority_id}
                    rowselection={rowSelection}
                    dataSource={list}
                    pagination={paginationProps}
                    onChange={this.handleTableChange}
                    onExpandedRowsChange={this.onExpandedRowsChange}
                    expandedRowKeys={this.state.expandedRowKeys}
                    columns={columns}
                    scroll={{x:1500}}
                >

                </Table>
            </div>
        );
    }
}